﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QxFramework.Core;
using GameControl;
namespace GameCombine
{   
    public class ItemCombineManager : LogicModuleBase, IItemCombineManager
    {

        List<ItemJointField> itemJointFields = new List<ItemJointField>();

        public List<ItemJointField> ItemJointFields => itemJointFields;

        public override void Init(object args)
        {
            base.Init(args);
        //    MessageManager.Instance.Get<>
        }
        public void AddJoint(ItemJointField joint )
        {
            if(itemJointFields.Find(s => (s.Item[0]==joint.Item[0]&&s.Item[1]== joint.Item[1]) || (s.Item[1] == joint.Item[0] && s.Item[0] == joint.Item[1])) != null)
            {
                Debug.LogWarning("有重复的连接点");
                return;
            }
            itemJointFields.Add(joint);
        }
        ItemLayoutState CheckIfCoreSettingRight(List<CombinableItemField> items)
        {
            var coreList = items.FindAll(x =>
            {
                return GameManager.Get<IItemInstantiateManager>().GetItemSettingByName(x.ItemName).GetComponent<Interact>() != null;
            });
            if (coreList.Count == 0)
            {
                return ItemLayoutState.CoreNotFound;
            }
            foreach (var core in coreList)
            {
                List<FuncField> funcs = GameManager.Get<IItemFuncManager>().GetFuncFieldAbout(core.ItemName);
                foreach (var x in funcs)
                {
                    
                    if (typeof(Interact).IsAssignableFrom(System.Type.GetType(x.FuncTypeName))){
                        if (x.Key == KeyCode.None)
                        {
                            return ItemLayoutState.CoreNotSetting;
                        }
                    }
                }
            }
            return ItemLayoutState.Successfull;
        }

        public ItemLayoutState GenItemJoint(List<CombinableItemField> items)
        {
            if (CheckIfOverlapped(items))
            {
                return ItemLayoutState.Overlapped;
            }
            var state = CheckIfCoreSettingRight(items);
            if (state != ItemLayoutState.Successfull)
            {
                return state;
            }

            List<ItemJointField> list = new List<ItemJointField>();
           
            foreach(var x in items)
            {
                for(int i = 0; i < 4; i++)
                {
                    if ((x.ConnectState & (1 << i)) == 0) continue;
                    var tp = items.Find(s => s.MeshTransform.BenchMeshPos == Item.GetMeshPosInWorldDirection(x, i));
                    if (tp != null)
                    {
                        for(int j = 0; j < 4; j++)
                        {

                            if ((tp.ConnectState & (1 << j)) == 0) continue;
                            if (x.MeshTransform.BenchMeshPos == Item.GetMeshPosInWorldDirection(tp, j))
                            {
                                list.Add(new ItemJointField
                                {
                                    Item = new string[2] { x.ItemName, tp.ItemName },
                                    JointPos = new int[2] { i, j }
                                });
                                break;
                            }
                        }
                    }
                }
            }
            foreach(var x in list)
            {
                AddJoint(x);
            }
            return ItemLayoutState.Successfull;
        }
        bool CheckIfOverlapped(List<CombinableItemField> items)
        {
            Vector3Int siz = CombineManager.Get<ICombineUIManager>().CombineTableSize;
            for(int z=-1;z<=1;z++)
            for (int x = 1; x <= siz.x; x++)
            {
                for (int y = 1; y <= siz.y; y++)
                {
                    var tp = items.FindAll(s => s.MeshTransform.BenchMeshPos == new Vector3Int(x, y, z));
                    if (tp != null && tp.Count > 1)
                    {
                        return true;
                    }
                }
            }
                return false;
        }

        public void RemoveJointAbout(string name)
        {
            itemJointFields.RemoveAll(s => s.Item[0] == name || s.Item[1] == name);
        }

        public string GetItemInJointDir(string thisName, int dir)
        {
            var field = ItemJointFields.Find(s => s.Item[0] == thisName && s.JointPos[0] == dir);
            if (field!=null)
            {
                return field.Item[1];
            }
            field = ItemJointFields.Find(s => s.Item[1] == thisName && s.JointPos[1] == dir);
            if (field != null)
            {
                return field.Item[0];
            }
            return "";
        }

        public bool CheckIfConnected(string a, string b)
        {
            if (itemJointFields.Find(s => (s.Item[0] == a && s.Item[1] == b) || (s.Item[1] == a && s.Item[0] == b)) == null){
                return false;
            }
            else
            {
                return true;
            }
        }
    }
    interface IItemCombineManager
    {
        void RemoveJointAbout(string name);
        List<ItemJointField> ItemJointFields { get; }
        void AddJoint(ItemJointField joint);
        ItemLayoutState GenItemJoint(List<CombinableItemField> items);

        string GetItemInJointDir(string thisName, int dir);
        bool CheckIfConnected(string a, string b);
        
    }
    public enum ItemLayoutState
    {
        Successfull,
        Overlapped,
        CoreNotFound,
        CoreNotSetting
    }
}